/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.server;

import swym.server.events.*;
import swym.server.config.*;
import swym.server.io.*;

import java.util.*;
import java.io.*;

/**
 *
 * @author mschaefer
 */
public class UserManager implements ServerListener
{
    private final static String USER_FILE = Settings.getBasePath() + "users";
    private final static String SEPARATOR = ":";
    
    private static UserManager instance = new UserManager();
    
    private HashMap<String, User> users;
    
    public static UserManager getInstance() { return instance; }
    
    private UserManager()
    {
        users = new HashMap<String, User>();
        
        loadUserInfo();
        
        Server.addListener( this );
    }
    
    /**
     * Pull in all of the user/client information from the users file.
     * 
     * the format is something like:
     * user0:client0
     * user0:client1
     * user1:client0
     * etc...
     */
    private void loadUserInfo()
    {
        try
        {
            Vector<String> lines = FileUtilities.readLines( USER_FILE );

            for ( String line : lines )
            {
                int separator = line.indexOf( SEPARATOR );
                
                if ( separator > 0 )
                {
                    String userName   = line.substring( 0, separator );
                    String clientName = line.substring( separator + 1 );
                    
                    User user = getUser( userName );
                    
                    if ( user == null )
                    {
                        user = new User( userName );
                        registerUser( user );
                    }
                    
                    Client client = user.getClient( clientName );
                    
                    if ( client == null )
                    {
                        client = new Client( clientName );
                        user.addClient( client );
                    }
                }
            }
        }
        catch ( IOException e ) {}
    }
    
    /**
     * persists the user information to the disk (the users file)
     */
    private void writeUserInfo()
    {
        try
        {
            synchronized ( users )
            {
                Vector<String> fileLines = new Vector<String>();
                Vector<String> userNames = new Vector<String>( users.keySet() );
                
                for ( String userName : userNames )
                {
                    User user = getUser( userName );
                    
                    for ( Client client : user.getClients() )
                        fileLines.add( userName + SEPARATOR + client.getName() );
                }
                
                FileUtilities.writeLines( USER_FILE, fileLines );
            }
        }
        catch ( IOException e )
        {
            System.out.println( "error saving user file: " + e.getMessage() );
        }  
    }
    
    /**
     * Add the user to the main user list.
     * @param user
     */
    public void registerUser( User user )
    {
        synchronized ( users )
        {
            users.put( user.getName(), user );
        }
    }
    
    public User getUser( String userName )
    {
        synchronized ( users )
        {
            return users.get( userName );
        }
    }
    
    /**
     * Handles server shutdown
     */
    public void handleShutdown()
    {
        System.out.println( "user mgr handling shutdown");
        // persist user stuff
        writeUserInfo();
    }
    
    public void printStatus()
    {
        System.out.println( "UserManager Status");
        synchronized ( users )
        {
            Vector<String> userList = new Vector<String>( users.keySet() );
            
            for ( String userName : userList )
            {
                users.get( userName ).printStatus();
            }
        }
    }
}
